// ConvertRaw.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "CompositeIFstream.h"

void GenRawFromRZData(_TCHAR* sInputDir, _TCHAR* sOutputDir);
void GenRawFromEikonalData(_TCHAR* sInputDir, _TCHAR* sOutputDir);

int _tmain(int argc, _TCHAR* argv[])
{
	if(argc < 3)
	{
		printf("Use:");
		return 0;
	}

	_TCHAR* sInputDir = new _TCHAR[MAX_PATH];
	_TCHAR* sOutputDir = new _TCHAR[MAX_PATH];
	int iWhichDataType = 0;

	//copy directory string
	memset(sInputDir,0,sizeof(_TCHAR)*MAX_PATH);
	_tcscpy(sInputDir, argv[1]);
	_tprintf(_T("Input folder is: %s\n"),sInputDir);

	//determine which kind of data for conversion
	//0 for renzhong's data
	//1 for eikonal data
	iWhichDataType = _tstoi(argv[2]);
	if(iWhichDataType<0) iWhichDataType=0;
	if(iWhichDataType>1) iWhichDataType=1;
	_tprintf(_T("Current Data Type is: %d\n"),iWhichDataType);

	//prepare output directory
	memset(sOutputDir, 0, sizeof(_TCHAR)*MAX_PATH);
	if(argv[3] == NULL)
		//use input folder
		_tcscpy(sOutputDir,argv[1]);
	else
		_tcscpy(sOutputDir,argv[3]);
	_tprintf(_T("Output folder is: %s\n"),sOutputDir);



	//do the conversion
	if(iWhichDataType == 0)
		GenRawFromRZData(sInputDir, sOutputDir);
	else if(iWhichDataType == 1)
		GenRawFromEikonalData(sInputDir, sOutputDir);

	delete[] sInputDir;
	delete[] sOutputDir;

	return 0;
}

void GenRawFromRZData(_TCHAR* sInputDir, _TCHAR* sOutputDir)
{
	if(sInputDir == NULL || sOutputDir == NULL)
		return;

	_TCHAR* sFullSearchPath = new _TCHAR[MAX_PATH];
	memset(sFullSearchPath,0,sizeof(_TCHAR)*MAX_PATH);
	_tcscpy(sFullSearchPath, sInputDir);

	_TCHAR* sPattern = _T("*.vsq");
	_tcscat(sFullSearchPath, sPattern);

	WIN32_FIND_DATA FindData;
	HANDLE hFind;

	hFind = FindFirstFile( sFullSearchPath, &FindData );

	if( hFind == INVALID_HANDLE_VALUE )
	{
		_tprintf(_T("Error searching directory\n"));
		return;
	}

	bool bFind = false;
	do{
		if(!_tcschr(FindData.cFileName,_T('#')))
		{
			bFind = true;
			break;
		}
	}while(FindNextFile(hFind, &FindData));

	if(!bFind)
	{
		_tprintf(_T("Error finding first file\n"));
		return;
	}

	_TCHAR* sFileName = new _TCHAR[MAX_PATH];
	memcpy(sFileName,sInputDir,sizeof(_TCHAR)*MAX_PATH);
	_tcscat(sFileName,FindData.cFileName);

	CCompositeIFstream iDataStream;
	iDataStream.Open(sFileName);

	//Save .raw format
	int iNumFrames = iDataStream.GetNumFrames();
	_tprintf(_T("The input sequence has %d frames\n"), iNumFrames);

	int nx = iDataStream.GetResolution();
	int ny = iDataStream.GetResolution();
	int nz = iDataStream.GetResolution();
	_tprintf(_T("The input volume res. is %dx%dx%d"),nx,ny,nz);

	if(iNumFrames == 0||nx == 0)
	{
		_tprintf(_T("Error in input file!"));
		return;
	}
	float dx = 1.0f/(nx);

	_TCHAR* sPrefix = new _TCHAR[MAX_PATH];
	memset(sPrefix,0,sizeof(_TCHAR)*MAX_PATH);
	_tcscpy(sPrefix,FindData.cFileName);
	_TCHAR* pos = _tcschr(sPrefix,_T('.'));
	*pos = _T('\0');

	_TCHAR* sSuffix = new _TCHAR[20];
	memset(sSuffix,0,sizeof(_TCHAR)*20);

	int iCurrentFrame = 0;
	float* fFramedata = new float[nx*ny*nz];
	do{
		//create file name
		memset(sFileName,0,sizeof(_TCHAR)*MAX_PATH);
		_tcscpy(sFileName,sOutputDir);
		_stprintf(sSuffix,_T("%03d.raw"),iCurrentFrame);
		_tcscat(sFileName,sPrefix);
		_tcscat(sFileName,sSuffix);

		FILE* fp = NULL;
		if( (fp  = _tfopen( sFileName, _T("wb"))) == NULL ) 
		{
			_tprintf(_T("The file can not opened\n") );
			return;
		}

		fwrite((const char *)&nx,sizeof(nx),1,fp);
		fwrite((const char *)&ny,sizeof(ny),1,fp);
		fwrite((const char *)&nz,sizeof(nz),1,fp);
		fwrite((const char *)&dx,sizeof(dx),1,fp);

		iDataStream.GetFrame(iCurrentFrame,fFramedata);
		fwrite((const char *)fFramedata,sizeof(float),nx*ny*nz,fp);		

		fclose(fp);
		_tprintf(_T("The frame %d saved\n"),iCurrentFrame);

		iCurrentFrame++;
	}
	while(iCurrentFrame<iNumFrames);

	delete[] fFramedata;
	iDataStream.Close();

	delete[] sFullSearchPath;
	delete[] sFileName;
	delete[] sPrefix;
	delete[] sSuffix;
}

void GenRawFromEikonalData(_TCHAR* sInputDir, _TCHAR* sOutputDir)
{
	if(sInputDir == NULL || sOutputDir == NULL)
		return;

	_TCHAR* sFullSearchPath = new _TCHAR[MAX_PATH];
	memset(sFullSearchPath,0,sizeof(_TCHAR)*MAX_PATH);
	_tcscpy(sFullSearchPath, sInputDir);

	_TCHAR* sPattern = _T("*.fdsc");
	_tcscat(sFullSearchPath, sPattern);

	WIN32_FIND_DATA FindData;
	HANDLE hFind;

	hFind = FindFirstFile( sFullSearchPath, &FindData );

	if( hFind == INVALID_HANDLE_VALUE )
	{
		_tprintf(_T("Error searching directory\n"));
		return;
	}
	_TCHAR* sFileName = new _TCHAR[MAX_PATH];
	memset(sFileName,0,sizeof(_TCHAR)*MAX_PATH);
	memcpy(sFileName,sInputDir,sizeof(_TCHAR)*MAX_PATH);
	_tcscat(sFileName,FindData.cFileName);

	FILE* fp = NULL;
	if( (fp  = _tfopen( sFileName, _T("r"))) == NULL ) 
	{
		_tprintf(_T("The file can not opened\n") );
		return;
	}

	//read related info!
	_TCHAR* sTmpStr = new _TCHAR[MAX_PATH];
	memset(sTmpStr,0,sizeof(_TCHAR)*MAX_PATH);
	_fgetts(sTmpStr,100,fp);
	int nx = 0;
	int ny = 0;
	int nz = 0;
	int tmp1 = 0;
	int tmp2 = 0;
	_ftscanf(fp,_T("Dim %d %d %d (%d %d)\n"),&nx,&ny,&nz,&tmp1,&tmp2);
	fclose(fp);
	float dx = (1.0f)/nx;
	delete[] sTmpStr;

	memset(sFullSearchPath,0,sizeof(_TCHAR)*MAX_PATH);
	_tcscpy(sFullSearchPath, sInputDir);
	_TCHAR* sPattern1 = _T("*.frgba");
	_tcscat(sFullSearchPath, sPattern1);

	hFind = FindFirstFile( sFullSearchPath, &FindData );
	if( hFind == INVALID_HANDLE_VALUE )
	{
		_tprintf(_T("Error searching directory\n"));
		return;
	}
	_TCHAR* sPrefix = new _TCHAR[MAX_PATH];
	_TCHAR* sSuffix = _T(".raw");
	//r,g,b,a 4 floats for each voxel
	float* fFramedata = new float[nx*ny*nz*4];	
	do{
		//read file
		memset(sFullSearchPath,0,sizeof(_TCHAR)*MAX_PATH);
		_tcscpy(sFullSearchPath, sInputDir);
		_tcscat(sFullSearchPath,FindData.cFileName);

		if( (fp  = _tfopen( sFullSearchPath, _T("rb"))) == NULL ) 
		{
			_tprintf(_T("The file can not opened\n") );
			return;
		}

		fread(fFramedata,nx*ny*nz*4,sizeof(float),fp);
		fclose(fp);

		//output file
		memset(sPrefix,0,sizeof(_TCHAR)*MAX_PATH);
		_tcscpy(sPrefix,FindData.cFileName);
		_TCHAR* pos = _tcschr(sPrefix,_T('.'));
		*pos = _T('\0');

		//create file name
		memset(sFileName,0,sizeof(_TCHAR)*MAX_PATH);
		_tcscpy(sFileName,sOutputDir);
		_tcscat(sFileName,sPrefix);
		_tcscat(sFileName,sSuffix);

		FILE* fp1 = NULL;
		if( (fp1  = _tfopen( sFileName, _T("wb"))) == NULL ) 
		{
			_tprintf(_T("The file can not opened\n") );
			return;
		}

		fwrite((const char *)&nx,sizeof(nx),1,fp1);
		fwrite((const char *)&ny,sizeof(ny),1,fp1);
		fwrite((const char *)&nz,sizeof(nz),1,fp1);
		fwrite((const char *)&dx,sizeof(dx),1,fp1);

		fwrite((const char *)fFramedata,sizeof(float),nx*ny*nz*4,fp1);		

		fclose(fp1);

		_tprintf(_T("The file %s saved\n"), sFileName);

	}while(FindNextFile(hFind, &FindData));

	
	delete[] fFramedata;
	delete[] sFullSearchPath;
	delete[] sFileName;
	delete[] sPrefix;
}
